package com.example.springbooteasycode.zjrc.socket;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

public class ClientMain {
    public static void main(String[] args) {
        try (Socket socket = new Socket("localhost", 8888)) {
            System.out.println("已连接到服务器：" + socket.getInetAddress());

            // 发送数据到服务器
            String data = "Hello, World!";
            byte[] bytes = data.getBytes();
            System.out.println("byte[]长度: " + bytes.length);
            OutputStream os = socket.getOutputStream();
            os.write(bytes);

            System.out.println("数据已发送：" + data);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
